//@Auth:zdl
package myLog

import (
	"github.com/gogf/gf/g"
	"github.com/gogf/gf/g/container/gtype"
	"github.com/gogf/gf/g/os/glog"
	"github.com/gogf/gf/g/os/gtime"
	"github.com/gogf/gf/g/util/gconv"
)

var logger *glog.Logger
var olderTime = gtype.String{}
var debugCh chan []interface{}
var printCh chan interface{}

func Init() {
	var debug = gconv.Bool(g.Config().Get("log.isDebug"))
	glog.SetDebug(debug)
	path := g.Config().Get("log.path")
	glog.SetPath(gconv.String(path))
	style := g.Config().Get("log.style")
	glog.SetFile(gconv.String(style))
	debugCh = make(chan []interface{}, 100000)
	printCh = make(chan interface{}, 100000)
	//mongo.Init()
	go LoopMogoChan()

}

func LoopMogoChan() {
	for {
		select {
		case data := <-debugCh:
			getLogger().Debug(data...)
		case data := <-printCh:
			getLogger().Println(data)
		}
	}
}

func getLogger() *glog.Logger {
	if olderTime.Val() != gtime.Date() || logger == nil {
		logger = glog.Cat(gtime.Date())
		olderTime.Set(gtime.Date())
	}
	return logger
}

func Debug(v ...interface{}) {
	debugCh <- append(make([]interface{}, 0), v...)
}

func Println(v ...interface{}) {
	printCh <- append(make([]interface{}, 0), v...)
}

func Fatal(v ...interface{}) {
	getLogger().Fatal(v...)
}

func Error(v ...interface{}) {
	getLogger().Error(v...)
}
